package demo.intro;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;


public class TimerUtil {
	
    
    public void TimeCostSt(String className) {
        // 根据传入的类型名 查找带目标注解Timer的方法
        try {
            Class<?> c = Class.forName(className);
            Object obj = c.newInstance();
            Method[] methods = c.getDeclaredMethods();
            for (Method m : methods) {
                // 判断该方法是否包含Timer注解
                if (m.isAnnotationPresent(Timer.class)) {
                    m.setAccessible(true);
                    long start = System.currentTimeMillis(); //计时开始
                    // 执行该方法
                    m.invoke(obj);
                    long end = System.currentTimeMillis();  //计时结束
                    System.out.println( className + "." + m.getName() + "() time cost: " + String.valueOf(end - start) + " ms");
                }
            }
        } catch (IllegalAccessException | InstantiationException | InvocationTargetException | ClassNotFoundException e) {
            e.printStackTrace();
        } 
    }
    
}
